package com.goldstone.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.goldstone.dao.ColorDao;
import com.goldstone.dao.CompanyDao;
import com.goldstone.dao.PackDao;
import com.goldstone.dao.ProductDao;
import com.goldstone.dao.SizeDao;
import com.goldstone.entity.Color;
import com.goldstone.entity.PriceLog;
import com.goldstone.entity.Product;
import com.goldstone.entity.Size;
import com.goldstone.entity.code.PriceType;

@Component
@Transactional
public class ProductService {
	@Autowired
	private ProductDao dao;

	@Autowired
	private ColorDao colorDao;
	
	@Autowired
	private SizeDao sizeDao;

	@Autowired
	private PackDao packDao;
	
	@Autowired
	private CompanyDao companyDao;
	
	public void save(Product product) {
		initColor(product.getColors());
		initSize(product.getSizes());
		product.setColors(initColor(product.getColors()));
		product.setSizes(initSize(product.getSizes()));
		dao.save(product);
	}
	
	private List<Color> initColor(List<Color> colors) {
		List<Color> colors1 = new ArrayList<Color>();
		for(Color c : colors) {
			Color c1 = colorDao.findUniqueBy("name", c.getName());
			if(c1 == null) {
				colorDao.save(c);
				colors1.add(c);
			} else {
				colors1.add(c1);
			}
		}
		return colors1;
	}
	
	private List<Size> initSize(List<Size> sizes) {
		List<Size> sizes1 = new ArrayList<Size>();
		for(Size s : sizes) {
			Size s1 = sizeDao.findUniqueBy("name", s.getName());
			if(s1 == null) {
				sizeDao.save(s);
				sizes1.add(s);
			} else {
				sizes1.add(s1);
			}
		}
		return sizes1;
	}
	
	@Transactional(readOnly = true)
	public Product get(String id) {
		return dao.get(id);
	}
	@Transactional(readOnly = true)
	public void find(Page<Product> page, List<PropertyFilter> filters) {
		dao.findPage(page, filters);
	}
	public void delete(String id) {
		Product pro = dao.get(id);
		if(pro != null) {
			pro.setDeleted(true);
		}
		dao.save(pro);
	}
	
	public void addColor(String productId, Color color) {
		addColor(get(productId), color);
	}
	public void addColor(Product product, Color color) {
		product.addColor(color);
		dao.save(product);
	}
	
	public void addSize(String productId, Size size) {
		addSize(get(productId), size);
	}
	public void addSize(Product product, Size size) {
		product.addSize(size);
		dao.save(product);
	}
	public Product getByReffer(String ref) {
		return dao.findUniqueBy("ref", ref);
	}

	public void savePrice(String productId, PriceLog priceLog) {
		Product product = dao.get(productId);
		if(PriceType.SP.equals(priceLog.getPriceType())) {
			priceLog.setSupplier(companyDao.get(priceLog.getCompId()));
		} else {
			priceLog.setCustomer(companyDao.get(priceLog.getCompId()));
		}
		product.addPrice(priceLog);
		dao.save(product);
	}
	
}
